import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * @Description: TODO
 * @Author st
 * @Date 2021-12-01 16:50
 */
public class HrWriter {
    public void writeXml(){
        String file = "Z:\\Java_camp\\JavaWeb\\XML\\code\\src\\hr.xml";

        SAXReader reader = new SAXReader();

        try {
            Document document = reader.read(file);
            Element root = document.getRootElement();
            Element employee = root.addElement("employee");
            employee.addAttribute("no", "3311");
            Element name = employee.addElement("name");
            name.setText("ww");
            employee.addElement("salary").setText("3600");
            Element department = employee.addElement("department");
            department.addElement("dname").setText("hrDepart");
            Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HrWriter hrWriter = new HrWriter();
        hrWriter.writeXml();
    }
}
